package summer12.namespace;

import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


	
public class DungeoneersActivity extends Activity {
	
	CharacterCreation drawView;
	int storage = 0;
	String currentcharacter;
	String test = "No rolls yet!";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.main);
    }

    
    
    //this will call up the menu as formatted in res/layout/menu/menu.xml
    public boolean onCreateOptionsMenu(Menu menu){

    	MenuInflater inflater = getMenuInflater();

    	inflater.inflate(R.menu.menu, menu);

    	return true;

    	} 
    //goes back to home screen, probably better way to do this
    @Override
    public void onBackPressed() {
        setContentView(R.layout.main);
        return;
    }

    
    public void addname(View view) {
    	
        EditText et=(EditText) findViewById(R.id.editText1);
        String str_value = et.getText().toString();

        currentcharacter = str_value;
        setContentView(new CharacterCreation(this, str_value));
        
    	
    	
        
    }
    
    public void rollDice(int dice, int sides)
    {
    	
        int sum = 0;
        int rollResult =0;
        String rollResultsBuffer = "";
        int diceRemaining = dice;
        Random rng = new Random();
        if(sides > 0)
        {
            while(diceRemaining > 0)
            {
                diceRemaining--;
                rollResult = rng.nextInt(sides) + 1;
                sum+= rollResult;
                rollResultsBuffer += rollResult;
                if(diceRemaining > 0)
                {
                    rollResultsBuffer += " + "; // adds space and + sign if the current roll is not the last
                }
            }
        }
        test = rollResultsBuffer;
        storage = sum;
       
    }
    
    //res/layout/menu/menu.xml has the names of the ID called, use that as a guide
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.exit:
            	System.exit(0);
                return true;
            case R.id.make:
            	setContentView(R.layout.character);
                return true;
            case R.id.status:
            	rollDice(6, 20);
            	Toast.makeText(getApplicationContext(), test+"\nTotal out of 6 20D dice = "+storage, Toast.LENGTH_SHORT).show();
                return true;
            case R.id.combat:
            	setContentView(new CharacterCreation(this, test));
            	return true;
            case R.id.journal:
            	setContentView(new CharacterCreation(this, currentcharacter));
                    
                

            	

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}